package com.example.filedemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.security.Permission;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class MainActivity extends AppCompatActivity {
    private Button btn_create_file;
    private File file;
    private String[] PERMISSIONS_STORAGE = {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE};
//    private static final int WRITE_REQUEST_CODE = 43;
//
//    private static final int REQUEST_EXTERNAL_STORAGE = 1;
//    private static String[] PERMISSIONS_STORAGE = {
//            Manifest.permission.WRITE_EXTERNAL_STORAGE };
//
//    public static void verifyStoragePermissions(Activity activity) {
//        // Check if we have write permission
//        int permission = ActivityCompat.checkSelfPermission(activity,
//                Manifest.permission.WRITE_EXTERNAL_STORAGE);
//
//        if (permission != PackageManager.PERMISSION_GRANTED) {
//            // We don't have permission so prompt the user
//            ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
//                    REQUEST_EXTERNAL_STORAGE);
//        }
//    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_create_file = findViewById(R.id.btn_creat_file);
        btn_create_file.setOnClickListener(new View.OnClickListener() {
            private String TAG = "cxp";

            @Override
            public void onClick(View v) {
//                String save_path = Environment.getExternalStorageDirectory().toString() + "/video";
//                verifyStoragePermissions(MainActivity.this);
//                file = new File(save_path);
//                if(!file.exists()){
//                    Log.e("cxp", "onClick: " + "file is not exist" );
//                    file.mkdirs();
//                }
//                Log.e(TAG, "onClick: " + save_path );
//                SimpleDateFormat smt = new SimpleDateFormat("yyyy_MM_dd_HHmmss");
//                Date date = new Date(System.currentTimeMillis());
//                String filename = "/"+smt.format(date)+".mp4";
//                save_path = save_path +filename;
//                file = new File(save_path);
//                if(!file.exists()){
//                    try {
//
//                        file.createNewFile();
//                        Log.e(TAG, "onClick: " + "create file successed!");
//                    } catch (IOException e) {
//                        Log.e(TAG, "onClick: " + "create file filed!");
//                        e.printStackTrace();
//                    }
//                }


                //Java代码的方式
                    //检查运行时权限
                SaveFileJava sfj = new SaveFileJava();
                    if(ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
                        ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS_STORAGE,2);
                    }else{
                        try {
                            sfj.save("hell world");
                            Toast.makeText(MainActivity.this,"写入success",Toast.LENGTH_SHORT).show();
                        } catch (IOException e) {
                            Toast.makeText(MainActivity.this,"写入failed",Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        }

                    }
            }
        });
    }
}